<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
  <title>确认识别结果</title>
  <link rel="stylesheet" href="{{ asset('css/weui.min.css') }}"/>
  <link rel="stylesheet" href="http://cdn.amazeui.org/amazeui/2.7.2/css/amazeui.min.css">
  <link rel="stylesheet" href="{{ asset('css/example.css') }}">
  <script src="{{ asset('js/zepto.min.js') }}"></script>
  <script type="text/javascript" src="{{ asset('js/weui.min.js') }}"></script>
</head>
<body>
<div class="page preview js_show">
  <div class="page__hd">
    <h1 class="page__title">确认识别结果</h1>
    <p class="page__desc">请确认识别结果后点击确认按钮。注意：如果识别错误说明拍照不规范，请删除对应项后重新拍照上传。</p>
  </div>
  <div class="page__bd">
    @foreach($results as $result)
      <div class="weui-form-preview">
        <div class="weui-form-preview__hd">
          <div class="weui-form-preview__item">
            <label class="weui-form-preview__label">快递单号</label>
            <em class="weui-form-preview__value">{{ $result['items'][4]['itemstring'] }}</em>
          </div>
        </div>
        <div class="weui-form-preview__bd">
          <div class="weui-form-preview__item">
            <label class="weui-form-preview__label">快递公司</label>
            <span class="weui-form-preview__value">{{ $result['items'][1]['itemstring'] }}</span>
          </div>
          <div class="weui-form-preview__item">
            <label class="weui-form-preview__label">手机号码</label>
            <span class="weui-form-preview__value phone" data-phone="{{ $result['items'][6]['itemstring'] }}">{{ $result['items'][6]['itemstring'] }}</span>
          </div>
          <div class="weui-form-preview__item">
            <label class="weui-form-preview__label">姓名</label>
            <span class="weui-form-preview__value name" data-name="{{ $result['items'][7]['itemstring'] }}">{{ $result['items'][7]['itemstring'] }}</span>
          </div>
        </div>
        <div class="weui-form-preview__ft">
          <a class="weui-form-preview__btn weui-form-preview__btn_primary" style="color: #f00;" href="javascript:">删除</a>
        </div>
      </div>
    @endforeach
  </div>
  <div class="weui-btn-area">
    <button class="weui-btn weui-btn_primary">确认</button>
  </div>
</div>
<script>
  $(function () {
    $('.weui-form-preview__btn').click(function () {
      $(this).parents('.weui-form-preview').remove();
      if(!$('.weui-form-preview').length) {
        $('.weui-btn-area .weui-btn').addClass('weui-btn_disabled').attr('disabled', true);
      }
    });
    $('.weui-btn-area .weui-btn').click(function () {
      var para = [];
      var reg = /^1[0-9]{10}$/;
      var hasErr = false;
      $('.weui-form-preview').each(function (index, element) {
        var phone = $(element).find('.phone').attr('data-phone');
        if(!reg.test(phone)) {
          weui.alert('错误的手机号码：' + phone, { title: '提示' });
          hasErr = true;
          return false;
        }
        var name = $(element).find('.name').attr('data-name');
        para.push({name: name, phone: phone});
      });
      if(!hasErr) {
        var url = "{{ url('/scan') }}";
        $.ajax({
          type: 'POST',
          url: url,
          data: {
            "_token": "{{ csrf_token() }}",
            "form_data": para,
          },
          success: function (data) {
            if(data.code === 0) {
              location.href = "{{ url('/scan/success') }}";
            }
          },
          error: function (reject) {
            location.href = "{{ url('/scan/failed') }}";
          }
        });
      }
    });
  });
</script>
</body>
</html>
